import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-11-05
 * Time: 14:32
 */

public class TestDemo1 {
    public static void main1(String[] args) {

        //普通的队列
        Queue<Integer> queue = new LinkedList<>();
        queue.add(1);
        queue.add(2);
        queue.add(3);
        //出队列
        System.out.println(queue.remove());

        //返回队首元素
        System.out.println(queue.element());
    }

    public static void main2(String[] args) {
        //双端队列
        Deque<Integer> deque = new LinkedList<>();
        //入队
        deque.offer(10);
        deque.offerLast(1); //默认的队尾入队
        deque.offerFirst(2); //这是从队首入队
        System.out.println(deque.peek());
        System.out.println(deque.peekFirst()); //出队 队首元素
        System.out.println(deque.peekLast()); //出队 队尾元素

        System.out.println("----------------");
        System.out.println(1%8);

    }

    public static void main3(String[] args) {
        MyCircularQueue queue = new MyCircularQueue(3);
        queue.push(12);
        queue.push(23);
        queue.push(34);
        System.out.println(queue.pop());
        queue.push(45);

    }

    public static void main4(String[] args) throws InterruptedException {
        MyCircularQueueL queueL = new MyCircularQueueL();
        queueL.push(1);
        queueL.push(2);
        queueL.push(3);
        queueL.push(1);
    }

    //自己实现的阻塞队列 实现一个生产者消费者模型
    public static void main(String[] args) {
        MyCircularQueueL queueL = new MyCircularQueueL();
        //生产者
        Thread t = new Thread(() -> {
            int num = 0;
            while (true){
                System.out.println("生产了: " + num);
                try {
                    queueL.push(num);
                    num++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t.start();

        Thread t2 = new Thread(() ->{
            while (true){
                try {
                    int num = queueL.pop();
                    System.out.println("消费了: " + num);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t2.start();
    }
}
